package com.vehicle.service.Impl;

import com.vehicle.domains.entity.SysPermissionEntity;
import com.vehicle.domains.entity.SysRoleEntity;
import com.vehicle.domains.entity.SysUserEntity;
import com.vehicle.domains.info.UserInfo;
import com.vehicle.domains.bo.QueryPermissionBo;
import com.vehicle.domains.bo.QueryRoleBo;
import com.vehicle.domains.bo.QueryUserBo;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.mapper.SysPermissionMapper;
import com.vehicle.mapper.SysRolePermissionMapper;
import com.vehicle.mapper.SysUserMapper;
import com.vehicle.mapper.SysUserRoleMapper;
import com.vehicle.service.SysRoleService;
import com.vehicle.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void addUser(SysUserEntity  user) {
        sysUserMapper.insertUser(user);
    }

    @Override
    public SysUserEntity selectByUsername(String username) {
        return sysUserMapper.selectUserByUsername(username);
    }

    @Override
    public SysUserEntity selectByUserId(Long id) {
        return sysUserMapper.selectUserById(id);
    }

    @Override
    public void updateUser(SysUserEntity updateSysUser) {
        int i = sysUserMapper.updateUserById(updateSysUser);
        if(i!=1){
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("用户未更新到数据库");
        }
    }

    @Override
    public void deleteUser(Long id) {
        int i = sysUserMapper.deleteUserById(id);
        if(i!=1){
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("用户未删除到数据库");
        }
    }

    @Override
    public List<SysUserEntity> selectUserList(QueryUserBo queryUserBo) {
        return sysUserMapper.selectUserList(queryUserBo);
    }

    @Override
    public UserInfo getUserRoleAndPermission(Long id) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        List<Long> roIds = sysUserRoleMapper.selectRoleIdsByUserId(id);
        QueryRoleBo queryRoleBo = new QueryRoleBo();
        queryRoleBo.setIds(roIds);
        List<SysRoleEntity> sysRoleEntityList = sysRoleService.selectRoleList(queryRoleBo);
        userInfo.setRoleEntityList(sysRoleEntityList);

        List<Long> permissions = sysRolePermissionMapper.selectPermissionIdsByRoleId(roIds);
        QueryPermissionBo permissionBo = new QueryPermissionBo();
        permissionBo.setIds(permissions);
        List<SysPermissionEntity> sysPermissionEntityList = sysPermissionMapper.selectPermissionList(permissionBo);
        userInfo.setPermissionEntityList(sysPermissionEntityList);
        return userInfo;
    }
}
